//
// Created by hongrui on 5/13/21.
//

#ifndef LIBEVENT_UDP_THEAD_FOR_TRANSACTION_HPP
#define LIBEVENT_UDP_THEAD_FOR_TRANSACTION_HPP
#include <errno.h>
#include <string.h>
#include <iostream>
#include <libnet.h>
#include "udp_thread.hpp"
using namespace std;
class udp_thead_for_transaction : public udp_thread {
public:
    udp_thead_for_transaction() = default;
    virtual ~udp_thead_for_transaction() = default;
public:
    virtual bool deal_udp_message(int fd) {
        char buf[buffer_size] = "";
        socklen_t size = sizeof(struct sockaddr);
        struct sockaddr_in client_addr = {0};
        int len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, &size);
        printf(buf);
        if (len < 0) {
            cerr << "server recv message error...!" << endl;
            printf("Oh dear, something went wrong with recvfrom()! %s\n", strerror(errno));
            return false;
        }
        if (0 == len) {
            cerr << "connection closed...!" << endl;
        }
        /*
        cout << "connection port = " << client_addr.sin_port << endl;
        cout << "connection ip = " << inet_ntoa(client_addr.sin_addr) << endl;
        cout << "server recv message len = " << len << endl;
        cout << "sever send back message now...!" << endl; */
        //sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, size);

        iCount++;

        return true;
    }
};

#endif //LIBEVENT_UDP_THEAD_FOR_TRANSACTION_HPP
